package com.pigs.pigscheckinginsystem.controller;

import com.pigs.pigscheckinginsystem.entity.CheckingIn;
import com.pigs.pigscheckinginsystem.model.ResultFormat;
import com.pigs.pigscheckinginsystem.util.EasyPoiUtils;
import com.pigs.pigscheckinginsystem.util.ExcelUtil;
import com.pigs.pigscheckinginsystem.util.ExportExcelUtil;
import com.pigs.pigscheckinginsystem.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author PIGS
 * @version 1.0
 * @date 2020/4/24 16:11
 * @effect :
 */
@RestController
public class ExcelController {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 从表格插入数据
     *
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/uploadExcel")
    public ResultFormat uploadExcel(@RequestParam("file") MultipartFile file) throws IOException {

        List<CheckingIn> checkingIns = EasyPoiUtils.importExcel(file, CheckingIn.class);
        for (CheckingIn checkingIn : checkingIns) {
            mongoTemplate.insert(checkingIn, "checkingIn");
        }

        return ResultUtil.success(checkingIns);
    }

}
